package cn.edu.neu.springbootdemo.action;

import cn.edu.neu.springbootdemo.core.Constants;
import cn.edu.neu.springbootdemo.model.Movie;
import cn.edu.neu.springbootdemo.model.Order;
import cn.edu.neu.springbootdemo.model.User;
import cn.edu.neu.springbootdemo.service.FileKt;
import cn.edu.neu.springbootdemo.service.Orderservice;
import cn.edu.neu.springbootdemo.service.UserService;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("/order")
public class OrderAction {
    @Resource(name = "userServiceImpl")
    UserService userService;

    @Resource(name = "orderServicelmpl")
    private Orderservice orderService;

    //商品订单查询
    @RequestMapping("/getSOrderList")
    public String getSOrderList(Model model){
        List<Order> list=orderService.getSOrderList();
        model.addAttribute("List",list);
        System.out.println("Action:"+list.toString());
        return "admin/showShopOrder";
    }

    //商品订单删除
    @RequestMapping("/deleteShopOrder")

    public String deleteShopOrder(@RequestParam(name = "order_id", required = false) String order_ids, Model model) {
        String[] order_id = order_ids.split(",");
        System.out.println(order_id.toString());
        for (String a : order_id) {
            Boolean an = orderService.deleteSOrder(Integer.parseInt(a));
        }
        return "forward:/order/getSOrderList";
    }
    @RequestMapping("/findSOrder")
    public String findSOrder(@RequestParam(name = "sorder_id", required = false) int sorder_id, Model model){

        List<Order> list = orderService.findSOrder(sorder_id);
        model.addAttribute("List", list);
        return "/admin/showShopOrder";
    }

    @RequestMapping("/findIdSOrder")
    public String findIdSOrder(@RequestParam(name = "order_id", required = false) int order_id, Model model){

        Order order = orderService.findIdSOrder(order_id);
        model.addAttribute("Order", order);
        return "/admin/showShopOrder";
    }

    //商品订单添加

    @RequestMapping("/addSOrder")
    public String addSOrder(Order order,HttpSession session){
        User user = (User) session.getAttribute(Constants.LOGIN_USER);
    System.out.println("Order+++++++++User:"+user.toString());

    order.setUser_id(user.getUser_id());
    order.setAllprice(order.getShop_price()*order.getNumber());
    System.out.println("Order+++++++++order:"+order.toString());
     orderService.addSOrder(order);

        return "/admin/result";
    }


    //电影订单查询
    @RequestMapping("/getMOrderList")
    public String getMOrderList(Model model){
        List<Order> list=orderService.getMOrderList();
        model.addAttribute("moiveList",list);
        System.out.println("Action:"+list.toString());
        return "admin/showMovieOrder";
    }

    //电影订单删除
    @RequestMapping("/deleteMOrder")

    public String deleteMOrder(@RequestParam(name = "forder_id", required = false) String forder_ids, Model model) {
        String[] forder_id = forder_ids.split(",");
        System.out.println(forder_id.toString());
        for (String a : forder_id) {
            Boolean an = orderService.deleteMOrder(Integer.parseInt(a));
        }
        return "forward:/order/getMOrderList";
    }
}
